﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Lime49.UI;

namespace LockCrypt.AutoType {
    /// <summary>
    /// Shows open windows in a drop down box.
    /// </summary>
    public partial class WindowChooser : UserControl {
        public static readonly DependencyProperty AvailableWindowsProperty = DependencyProperty.Register("AvailableWindows", typeof(IEnumerable<string>), typeof(WindowChooser), new PropertyMetadata(Enumerable.Empty<string>()));
        /// <summary>
        /// Gets or sets the names of windows available for selection.
        /// </summary>
        /// <value>The available window names.</value>
        public IEnumerable<string> AvailableWindows {
            get { return base.GetValue(AvailableWindowsProperty) as IEnumerable<string>; }
            set { 
                base.SetValue(AvailableWindowsProperty, value);
                btnRefreshWindows.Visibility = value != null && value.Any() ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public static readonly DependencyProperty SelectedWindowProperty = DependencyProperty.Register("SelectedWindow", typeof(string), typeof(WindowChooser), new PropertyMetadata(string.Empty));
        /// <summary>
        /// Gets or sets the name of the selected window.
        /// </summary>
        /// <value>The selected window name.</value>
        public string SelectedWindow {
            get { return Convert.ToString(base.GetValue(SelectedWindowProperty)); }
            set { base.SetValue(SelectedWindowProperty, value); }
        }

        /// <summary>
        /// Initializes a new <see cref="WindowChooser"/> to choose from all open windows.
        /// </summary>
        public WindowChooser() {
            InitializeComponent();
            NavigationCommands.Refresh.Execute(null, this);
        }

        /// <summary>
        /// Initializes a new <see cref="WindowChooser"/> to allow selection from a range of windows.
        /// </summary>
        /// <param name="availableWindows">The available windows.</param>
        public WindowChooser(IEnumerable<string> availableWindows) {
            InitializeComponent();
            this.AvailableWindows = availableWindows;
        }

        /// <summary>
        /// Selects the first window from the list when the control loads.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        public void WindowChooser_Loaded(object sender, RoutedEventArgs e) {
            if(AvailableWindows != null && string.IsNullOrEmpty(cboAutoTypeTargetWindow.Text) && AvailableWindows.Any())
                cboAutoTypeTargetWindow.SelectedIndex = 0;
        }

        /// <summary>
        /// Refreshes the list of open windows.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void RefreshWindows(object sender, ExecutedRoutedEventArgs e) {
            try {
                AvailableWindows = AutoTypeRecorder.GetWindows();
            } catch(Exception ex) {
                DialogBox.ShowAlert(Window.GetWindow(this), ex.Message, I18nUtils.GetString("Errors", "Error"));
            }
        }
    }
}
